import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/gestures.dart';
import 'package:flutter_syt/utils/dio_download_img.dart';

// import 'dart:ui' as ui;
import 'package:intl/intl.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/domain/epidemic/epidemic_health_list.dart';
import 'package:flutter_syt/generated/assets.dart';
import 'package:flutter_syt/widgets/auto_image.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';
import 'package:flutter_syt/utils/utils_other.dart';

class PhotoViewMoreScreen extends StatefulWidget {
  final List<String> imageRows;
  final List<File>? fileRows;
  final int defaultImage;

  PhotoViewMoreScreen(
      {required this.imageRows, this.fileRows, this.defaultImage = 0});

  @override
  PhotoViewMoreScreenState createState() => PhotoViewMoreScreenState();
}

class PhotoViewMoreScreenState extends State<PhotoViewMoreScreen> {
  LongPressDownDetails? downDetail;
  int tempSelect = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
            backgroundColor: Colors.black,
            appBar: AppBar(
              shadowColor: Colors.black,
              foregroundColor: Colors.white,
              backgroundColor: Colors.black,
              title: Text(
                '$tempSelect/${(widget.fileRows ?? widget.imageRows).length}',
                style: TextStyle(color: Colors.white),
              ),
            ),
            // body: Container(
            //     constraints: BoxConstraints.expand(
            //       height: MediaQuery.of(context).size.height,
            //     ),
            //     child: PhotoView(
            //       imageProvider: NetworkImage(widget.imageRows[0]),
            //     )),
            body:
                // GestureDetector(
                //     onLongPress: () {
                //       DLog('onLongPress');
                //       if (downDetail != null) {
                //
                //         UtilsOther.showBottomSaveImage(image: (widget.fileRows ?? widget.imageRows)[tempSelect - 1]);
                //         // UtilsOther.showPopupMeun(
                //         //     downDetail!.globalPosition,
                //         //     (widget.fileRows ?? widget.imageRows)[tempSelect - 1],
                //         //     context);
                //       }
                //     },
                //     onLongPressDown: (details) {
                //       downDetail = details;
                //     },
                //     child:
                Stack(
              children: [
                Positioned.fill(
                    child: PhotoViewGallery.builder(
                        builder: (BuildContext context, int index) {
                          return PhotoViewGalleryPageOptions(
                              filterQuality: FilterQuality.none,
                              initialScale: PhotoViewComputedScale.contained,
                              minScale: PhotoViewComputedScale.contained *
                                  (0.5 + index / 10),
                              maxScale: PhotoViewComputedScale.covered * 4.1,
                              // imageProvider: NetworkImage(widget.imageRows[index])
                              imageProvider: getImageProvider(index));
                        },

                        // scrollDirection: Axis.horizontal,
                        // enableRotation: false,
                        // // gaplessPlayback: true,
                        // // wantKeepAlive: true,

                        itemCount: (widget.fileRows ?? widget.imageRows).length,
                        // backgroundDecoration: BoxDecoration(color: Colors.black),
                        pageController: PageController(
                            initialPage: widget.defaultImage, keepPage: false),
                        onPageChanged: (index) {
                          if (mounted) {
                            setState(() {
                              tempSelect = index + 1;
                            });
                          }
                        })),
                Positioned(
                    left: 0,
                    right: 0,
                    bottom: ScreenUtil().bottomBarHeight + 12.w,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        GestureDetector(
                          onTap: () async {
                            var image = (widget.fileRows ??
                                widget.imageRows)[tempSelect - 1];
                            UtilsOther.saveImageToAlbum(image);
                          },
                          behavior: HitTestBehavior.opaque,
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              40.verticalSpaceFromWidth,
                              const AutoImage(
                                Assets.saveIcon,
                                width: 22,
                              ),
                              6.horizontalSpace,
                              Text(
                                S().save_image,
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16.sp,
                                  // fontWeight:
                                ),
                              )
                            ],
                          ),
                        )
                      ],
                    ))
              ],
            ))
        // )
        ;
  }

  getImageProvider(index) {
    if (widget.fileRows != null) {
      return FileImage(widget.fileRows![index]);
    } else {
      // ExtendedImageProvider CachedNetworkImageProvider
      return NetworkImage(widget.imageRows[index]);
    }
  }

  @override
  void initState() {
    super.initState();
    tempSelect = widget.defaultImage + 1;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(PhotoViewMoreScreen oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
